# -*- coding: utf-8 -*-

import datetime
import calendar
import time
import random
import hashlib

class DateAndTimeHandler:
    # discard
    def GetWeekDay(self):
        dayofweek = datetime.now().weekday()
        if dayofweek == 0:
            return "星期一"
        elif dayofweek == 1:
            return "星期二"
        elif dayofweek == 2:
            return "星期三"
        elif dayofweek == 3:
            return "星期四"
        elif dayofweek == 4:
            return "星期五"
        elif dayofweek == 5:
            return "星期六"
        else:
            return "星期日"

    #print GetWeekDay()
    # discard
    def GetMondayDate(self):
        curday = datetime.datetime.now()
        oneday = datetime.timedelta(days = 1)
        if curday.weekday() == calendar.SATURDAY:
            curday += oneday
        if curday.weekday() == calendar.SUNDAY:
            curday += oneday
        while curday.weekday() != calendar.MONDAY:
            curday -= oneday

        return curday
    
    # discard
    def GetFridayDate(self):
        curday = datetime.datetime.now()
        oneday = datetime.timedelta(days = 1)
        while curday.weekday() != calendar.FRIDAY:
            curday += oneday

        return curday

    #print(GetMondayDate().strftime("%Y-%m-%d,%a") + " ~ " + GetFridayDate().strftime("%Y-%m-%d,%a"))
    # discard
    def GetKYYInterval(self):
        return (self.GetMondayDate().strftime("%Y-%m-%d,%a") +
                " ~ " + self.GetFridayDate().strftime("%Y-%m-%d,%a"))
                
    def toDateSeqStr(self, timeseq):
        if timeseq == "1":
            return "周一"
        elif timeseq == "2":
            return "周二"
        elif timeseq == "3":
            return "周三"
        elif timeseq == "4":
            return "周四"
        elif timeseq == "5":
            return "周五"
        else:
            return "未知星期"

        return "未知星期"

    # discard
    def toTimeSeqStr(self, dateseq):
        if dateseq == "1":
            return "12:30-13:20"
        elif dateseq == "2":
            return "14:00-14:50"
        elif dateseq == "3":
            return "15:00-15:50"
        elif dateseq == "4":
            return "16:00-16:50"
        elif dateseq == "5":
            return "18:00-18:50"
        else:
            return "false"

        return "false"
                        
    def GetYYDateAndTime(self, dateseq):        
        #通过预约的星期获取其具体日期
        curday = datetime.datetime.now()
        oneday = datetime.timedelta(days = 1)
        
        intdateseq = 0
        try:
            intdateseq = int(dateseq)
        except:
            pass
        
        today = curday.weekday()
        nextweek = today==calendar.SATURDAY or today==calendar.SUNDAY
        if nextweek:
            while curday.weekday() != (int(dateseq)-1):
                curday += oneday
        elif today > (intdateseq-1):
            while curday.weekday() != (intdateseq-1):
                curday += oneday #此处策略是提前一周预约，如果只能预约本周的，则curday -= oneday
        elif today < (intdateseq-1):
            while curday.weekday() != (intdateseq-1):
                curday += oneday
        elif today == (intdateseq-1):
            curday += oneday
            while curday.weekday() != (intdateseq-1):
                curday += oneday
            
        strDate = curday.strftime("%Y-%m-%d")
        
        return strDate
        
    def GetYYDate_(self, dateseq):        
        #通过预约的星期获取其具体日期
        curday = datetime.datetime.now()
        oneday = datetime.timedelta(days = 1)
        
        intdateseq = 0
        try:
            intdateseq = int(dateseq)
        except:
            pass
        
        today = curday.weekday()
        nextweek = today==calendar.SATURDAY or today==calendar.SUNDAY
        if nextweek:
            while curday.weekday() != (int(dateseq)-1):
                curday += oneday
        elif today > (intdateseq-1):
            while curday.weekday() != (intdateseq-1):
                curday += oneday #此处策略是提前一周预约，如果只能预约本周的，则curday -= oneday
        elif today < (intdateseq-1):
            while curday.weekday() != (intdateseq-1):
                curday += oneday
        elif today == (intdateseq-1):
            curday += oneday
            while curday.weekday() != (intdateseq-1):
                curday += oneday
                
        return curday
        
    def GetTchDateAndTime(self, strdate):
        strweekday = self.toDateSeqStr(strdate)
        
        return u"" + strweekday
        
    def YYTimeToDateTime(self, strdate):
        #输入的strdate格式为:2016-11-30-09:45
        try:
            items = strdate.split('-')
            timeitems = items[-1].split(':')
            yytime = datetime.datetime(int(items[0]),int(items[1]),int(items[2]),int(timeitems[0]),int(timeitems[1]))
            return yytime
        except:
            return None
            
    def sleep_time(self,hour,minute,second):
        return hour*3600+minute*60+second
        
class SmsHandler:
    @classmethod
    def SendSmsMsg(self, phoneno, msg, tempno):
        #return "" 
        #print(phoneno, msg, tempno)
        """
        sendmsg = None
        try:
            sendmsg = unicode(msg).encode("utf-8")
            print(sendmsg)
        except Exception as e:
            print("encode error")
            print(e)
            sendmsg = msg
        """
        url="http://api.sms.cn/sms/?ac=send&uid=chinn&pwd=a394329ee8b4636a85240a2282288bc1&mobile=%s&content={\"product\":\"%s\"}&template=%d" % (phoneno, msg, tempno)
        #print(url)
        urlutf8 = None
        try:
            urlutf8 = unicode(url).encode("utf-8")
        except:
            urlutf8 = url
        #print(url1)
        try:
            import urllib.request
            #import sys
            #reload(sys)
            #sys.setdefaultencoding('UTF-8')
            request = urllib.request.urlopen(urlutf8)
            return request.read()
        except:
            try:
                import urllib
                request = urllib.urlopen(urlutf8)
                return request.read()
            except Exception as e:
                #print(e)
                pass
            
        return ""
        
    @classmethod
    def buildSmsMsg(self, phoneno, msg, tempno):
        #return "" 
        #print(phoneno, msg, tempno)
        """
        sendmsg = None
        try:
            sendmsg = unicode(msg).encode("utf-8")
            print(sendmsg)
        except Exception as e:
            print("encode error")
            print(e)
            sendmsg = msg
        """
        url="http://api.sms.cn/sms/?ac=send&uid=chinn&pwd=a394329ee8b4636a85240a2282288bc1&mobile=%s&content={\"product\":\"%s\"}&template=%d" % (phoneno, msg, tempno)
        
        return url
        
    @classmethod
    def SendSmsMsg2(self, url):    
        #print(url)
        urlutf8 = None
        try:
            urlutf8 = unicode(url).encode("utf-8")
        except:
            urlutf8 = url
        #print(url1)
        try:
            import urllib.request
            #import sys
            #reload(sys)
            #sys.setdefaultencoding('UTF-8')
            request = urllib.request.urlopen(urlutf8)
            return request.read()
        except:
            try:
                import urllib
                request = urllib.urlopen(urlutf8)
                return request.read()
            except Exception as e:
                #print(e)
                pass
            
        return ""  

    @classmethod
    def SendSmsMsg3(self, url):    
        #print(url)
        urlutf8 = None
        try:
            urlutf8 = unicode(url).encode("utf-8")
        except:
            urlutf8 = url
        #print(url1)
        try:
            import urllib.request
            #import sys
            #reload(sys)
            #sys.setdefaultencoding('UTF-8')
            request = urllib.request.urlopen(urlutf8)
            return request.read()
        except:
            try:
                import urllib
                request = urllib.urlopen(urlutf8)
                return request.read()
            except Exception as e:
                #print(e)
                pass
            
        return ""
        
    @classmethod
    def createVerifyCode(self): 
        chars=['0','1','2','3','4','5','6','7','8','9'] 
        x = random.choice(chars),random.choice(chars),random.choice(chars),random.choice(chars) 
        verifyCode = "".join(x) 
        return verifyCode 
        
class NjuUtility:
    @classmethod
    def GetYYContent(self, index):
        if int(index) == 1:
            return u"职业生涯规划"
        elif int(index) == 2:
            return u"求职与职场心理咨询"
        elif int(index) == 3:
            return u"礼仪与应聘技巧咨询"
        elif int(index) == 4:
            return u"求职方法与择业咨询"
        elif int(index) == 5:
            return u"行业和重点单位咨询"

        return u""
        
    @classmethod
    def GetYYStatus(self, index):
        num = int(index)
        ret = u"未知状态"
        if num==1:
            ret = u"等待咨询"
        elif num==2:
            ret = u"咨询完成"
        elif num==3:
            ret = u"咨询取消"
        
        return ret
        
    @classmethod
    def GetStuStatus(self, index):
        num = int(index)
        ret = u"未知状态"
        if num==1:
            ret = u"注册成功"
        elif num==2:
            ret = u"实名登记"
        elif num==3:
            ret = u"限制预约"
        elif num==4:
            ret = u"咨询师安排"
        
        return ret

    @classmethod
    def GetArticleType(self, index):
        num = int(index)
        ret = u"未知类型"
        if num == 1:
            ret = u"咨询师探秘"
        elif num == 2:
            ret = u"咨询室故事分享"
        elif num == 3:
            ret = u"求职助力"

        return ret
        
    @classmethod
    def GetConsultantImagePath(self,id,contact):
        return "_"+str(id)+"_"+str(contact)
            
    @classmethod
    def md5Encode(self, target):
        try:
            m = hashlib.md5()
            m.update(target)
            x = m.hexdigest()
            
            z = x + x
            
            m2 = hashlib.md5()
            m2.update(z)
            
            return m2.hexdigest()
        except:
            m = hashlib.md5()
            m.update(target.encode("utf-8"))
            x = m.hexdigest()
            
            z = x + x
            
            m2 = hashlib.md5()
            m2.update(z.encode('utf-8'))
            return m2.hexdigest()
